package top.humbleyuan.util;

import java.util.*;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.CopyOnWriteArrayList;
import java.util.concurrent.locks.Lock;

/**
 * @Author HumbleYuan
 * @Date 2020/3/29 17:04
 * @Des 有关数学的相关方法
 */
public class MathUtil {
    public static void main(String[] args) {
        //System.out.println(minCommonMultiple(8, 12));

        System.out.println(scaleTransfer(13, 16));



    }

    /**
     * 最小共倍数
     * @param num1
     * @param num2
     * @return
     */
    public static int minCommonMultiple(int num1,int num2) {

        if(num1 == num2) {
            return num1;
        }

        int min = Math.min(num1, num2);
        int div = 1;
        // 先找到所有公因数约去
        for (int i = 2; i <= (min >> 1); i++) {
            // 计算最大公因数
            if(num1 % i == 0 && num2 % i == 0) {
                div = i;
            }
        }

        // 最小公倍数
        return num1 * num2 / div;
    }


    /**
     * 最大公约数
     * @param num1
     * @param num2
     * @return
     */
    public static int maxCommonDivisor(int num1, int num2) {
        if(num1 == num2) {
            return num1;
        }

        int min = Math.min(num1, num2);
        int div = 1;
        // 先找到所有公因数约去
        for (int i = 2; i <= (min >> 1); i++) {
            // 计算最大公因数
            if(num1 % i == 0 && num2 % i == 0) {
                div = i;
            }
        }

        return div;
    }

    /**
     * 十进制转二进制，八进制，十六进制
     * @param num
     * @return
     */
    public static String scaleTransfer(int num, int type) {
        if(type == 16) {
            return sixteenTransfer(num);
        }

        int div = type;

        String res = "";

        while (num > 0) {
            int remain = num % div;

            num /= div;
            res += remain;
        }

        res = new StringBuilder(res).reverse().toString();

        return res;

    }


    public static String sixteenTransfer(int num) {
        HashMap<Integer, Character> map = new HashMap<>();
        for (int i = 10; i <= 15; i++) {
            map.put(i, (char) (i + 55));
        }

        String res = "";
        while (num > 0) {
            int remain = num % 16;
            num /= 16;

            if(remain >= 10) {
                res += map.get(remain);
                continue;
            }

            res += remain;
        }

        return res;
    }
}
